import request from '@/utils/request'

// 获取资料列表
export function getMaterials(params) {
  return request({
    url: '/materials',
    method: 'get',
    params
  }).then(res => {
    if (res.data && Array.isArray(res.data)) {
      res.data = res.data.map(processMaterial)
    }
    return res
  })
}

// 处理图片URL
function processMaterial(material) {
  if (material.thumbnailPath && material.thumbnailPath.includes('via.placeholder.com')) {
    material.thumbnailPath = '/images/default-thumbnail.jpg'
  }
  return material
}

// 获取资料详情
export function getMaterialById(id) {
  return request({
    url: `/materials/${id}`,
    method: 'get'
  }).then(res => {
    if (res.data) {
      res.data = processMaterial(res.data)
    }
    return res
  })
}

// 上传资料
export function uploadMaterial(data, config = {}) {
  return request({
    url: '/materials',
    method: 'post',
    data,
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    ...config
  })
}

// 更新资料
export function updateMaterial(id, data) {
  return request({
    url: `/materials/${id}`,
    method: 'put',
    data
  })
}

// 删除资料
export function deleteMaterial(id) {
  return request({
    url: `/materials/${id}`,
    method: 'delete'
  })
}

// 获取分类列表
export function getCategories() {
  return request({
    url: '/categories',
    method: 'get'
  })
}

// 获取标签列表
export function getTags() {
  return request({
    url: '/tags',
    method: 'get'
  })
}

// 获取热门资料
export function getHotMaterials(limit = 4) {
  return request({
    url: '/materials',
    method: 'get',
    params: {
      sort: 'downloads',
      limit
    }
  })
}

// 获取最新资料
export function getLatestMaterials(limit = 4) {
  return request({
    url: '/materials',
    method: 'get',
    params: {
      sort: 'latest',
      limit
    }
  })
}

// 获取相关资料
export function getRelatedMaterials(id, limit = 4) {
  return request({
    url: `/materials/${id}/related`,
    method: 'get',
    params: { limit }
  })
}

// 获取用户下载记录
export function getUserDownloads(params) {
  return request({
    url: '/materials/downloads',
    method: 'get',
    params
  })
}

// 生成下载链接
export function generateDownloadLink(id, options = {}) {
  return request({
    url: `/materials/${id}/download`,
    method: 'post',
    data: options
  })
}

// 创建材料订单
export function createMaterialOrder(data) {
  return request({
    url: '/materials/orders',
    method: 'post',
    data
  })
}

// 获取材料订单详情
export function getMaterialOrder(orderNumber) {
  return request({
    url: `/materials/orders/${orderNumber}`,
    method: 'get'
  })
}

// 获取用户的材料订单列表
export function getMaterialOrders(params) {
  return request({
    url: '/materials/orders',
    method: 'get',
    params
  })
}

// 取消材料订单
export function cancelMaterialOrder(orderNumber) {
  return request({
    url: `/materials/orders/${orderNumber}/cancel`,
    method: 'post'
  })
}

// 生成材料下载令牌
export function generateDownloadToken(orderNumber) {
  return request({
    url: `/materials/orders/${orderNumber}/download`,
    method: 'post'
  })
}